var nsP = new nsPing();
var ew = new echoHttp();
var nsF = new nsFetch();
var config;
global["fetch"] = fetch; //for d3
nsF.on("result", (target, data) => {
data.shift();
let dd = summary(data);
dd['target'] = target.split(":")[1].substr(2,100);
dd['type'] = 'fetch';
dd['logtime'] = new Date().toISOString();
dd['host'] = hostname
dp.update([{k:Date.now(),v:dd}]);
dp.merge();
console.log("fetch==>",target, summary(data));
collectData()
});
nsP.on("result", (target, data) => {
data['type'] = 'ping';
data['logtime'] = new Date().toISOString();
data['host'] = hostname;
dp.update([{k:Date.now(),v:data}]);
dp.merge();
console.log("ping==>",target, data);
collectData();
});
var targets = []
var dp = new pool(dataServer, () => {
dp.openSocket();
dp.onSocketDataUpdate = d => {
dp.update(d);
dp.mergeLocal(false);
};
ew.launch(echoPort);
dp.update([{ k: hostname, v: { ip: adds, httpEchoPort: echoPort } }]);
dp.merge();
//console.log(dp.dump());
config = dp.get('config');
setInterval(() => {
targets = [];
dp.dump().filter(d=>d.v['ip']).forEach(t=>{
console.log(t.v);
(<string[]>t.v["ip"]).forEach(d => {
targets.push({type:"ping",ip:d});
targets.push({type:"fetch",ip:d,port:t.v['httpEchoPort']});
});
});
console.log("targets");
console.log(targets);
collectData();
}
, config.resampleFreq)
});
function collectData() {
//console.log(targets);
let target:{type:string,ip:string,port:number} = targets.shift();
console.log(target);
if (target)
if (target.type === 'ping') nsP.sample(target.ip,config.sampleSize);
else nsF.sample(`http://${target.ip}:${target.port}`,config.smallDataSize, config.sampleSize,config.delay);
// targets[currentTargetIP].
}